import * as fs from "fs";

const Data = {};

function loadJSON(pathname, key) {
  console.log(`dpsv1: loading ${pathname} -> ${key}`);
  Data[key] = JSON.parse(fs.readFileSync(pathname, "utf-8"));
}

function loadData(key, basepath) {
  loadJSON(`${basepath}/customdata/${key}.json`, key);
}

function loadExcel(key, base) {
  loadJSON(`${base}/excel/${key}.json`, key);
}

function loadCustomData(basepath) {
  ["version", "dps_options", "dps_specialtags", 
   "dps_anim", "green", "subclass"].forEach(
    x => loadData(x, basepath));
}

function loadGameData(basepath) {
  [
    "character_table",
    "char_patch_table",
    "skill_table",
    "uniequip_table",
    "battle_equip_table"
  ].forEach(x => loadExcel(x, basepath));
}

function patchChar(charId, patchId, suffix) {
  if (!Data.character_table[charId]) {
    Data.character_table[charId] = Data.char_patch_table["patchChars"][patchId];
    Data.character_table[charId].name += suffix;
    console.log("patch char", charId);
  }
}

function patchAllChars() {
  patchChar("char_1001_amiya2", "char_1001_amiya2", "（近卫）");
}

// 无法处理语言。但是dpsv1只用数据，和语言关系不大吧
function loadAll(lang="zh_CN") {
  // 实际工作目录为根
  loadGameData(`api/ArknightsGameData/${lang}/gamedata`);
  loadCustomData('api');
  patchAllChars();
}

loadAll();

export { Data }

